#include <iostream>
#include <signal.h>
#include <cstring>
#include "myh/MyLog.hpp"
enum{
    Penging
    ,Block
};

class SigCtl
{
    typedef int type_set;
public:
    SigCtl()
        :_setpending(new sigset_t)
        ,_setblock(new sigset_t)
    {
        SetEmpty(_setpending);
        SetEmpty(_setblock);
    }
    ~SigCtl()
    {
        delete _setpending;
    }

    void addset(int sig)
    {
        if(sigaddset(_setblock,sig) < 0)
        {
            lg(Error,"[%s-%d-sigaddset][%s]",__FILE__,__LINE__,strerror(errno));
            exit(errno);
        }
    }

    void setblock(int how = SIG_BLOCK,sigset_t* oldset = nullptr)
    {
        if(sigprocmask(how,_setblock,oldset) < 0)
        {
            lg(Error,"[%s-%d-sigprocmask][%s]",__FILE__,__LINE__,strerror(errno));
            exit(errno);
        }
    }
    
    sigset_t& getpending()
    {
        sigpending(_setpending);
        return *_setpending;
    }

    bool sigpengingempty(int sig)
    {
        int ret = sigismember(&getpending(),sig);
        if(ret < 0)
        {
            lg(Error,"[%s-%d-sigismember][%s]",__FILE__,__LINE__,strerror(errno));
            exit(errno);
        }
        else if(ret == 1){return true;}
        else {return false;}
    }

    void sigdeleteset(int sig)
    {
        if(sigdelset(_setblock,sig) < 0)
        {
            lg(Error,"[%s-%d-sigdelset][%s]",__FILE__,__LINE__,strerror(errno));
            exit(errno);
        }
    }

private:
    void SetEmpty(sigset_t*& set)
    {
        if(sigemptyset(set) < 0)
        {
            lg(Error,"[%s-%d-sigemptyset][%s]",__FILE__,__LINE__,strerror(errno));
            exit(errno);
        }
    }

    sigset_t* _setpending;
    sigset_t* _setblock;
};